package com.ylkj.io.stream.datahub;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.datahub.client.model.*;
import com.hwl.datahub.out.IDatahubRecordResolver;
import com.ylkj.bus.app.report.IotmoAppDO;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: wengzx
 * @Date: 2025/4/2
 * @Desc:
 */
@RequiredArgsConstructor
@Slf4j
public class SinkResolver implements IDatahubRecordResolver<List<IotmoAppDO>> {
    private static final String FIELD_CONTENT = "content";
    private RecordSchema recordSchema;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private String convert(List<IotmoAppDO> iotmoAppDOS) {
        if (iotmoAppDOS == null || iotmoAppDOS.isEmpty()) {
            log.warn("跳过空的 IotmoAppDO 列表。");
            return null;
        }

        JSONObject mainJsonObject = new JSONObject();
        JSONArray appStatusParamArray = new JSONArray();
        String deviceCode = ""; // 默认值
        String eventTime = getFormattedCurrentTime();

        for (IotmoAppDO iotmoAppDO : iotmoAppDOS) {
            if (iotmoAppDO == null) {
                continue;
            }

            // 提取 deviceCode，假设所有对象的 deviceCode 一致，取第一个非空值
            if (deviceCode.equals("") && iotmoAppDO.getDeviceIdentifier() != null) {
                deviceCode = iotmoAppDO.getDeviceIdentifier();
            }

            // 构建 appStatusParam 数组中的每个对象
            JSONObject appStatusJsonObject = new JSONObject();
            appStatusJsonObject.put("appName", iotmoAppDO.getAppName() != null ? iotmoAppDO.getAppName() : " ");
            appStatusJsonObject.put("serviceStatus", iotmoAppDO.getServiceStatus() != null ? iotmoAppDO.getServiceStatus() : " ");
            appStatusParamArray.add(appStatusJsonObject);
        }

        // 构建最终的 JSON 对象
        mainJsonObject.put("deviceCode", deviceCode);
        mainJsonObject.put("eventTime", eventTime);
        mainJsonObject.put("appStatusParam", appStatusParamArray);

        return mainJsonObject.toString();
    }

    private String getFormattedCurrentTime() {
        return dateFormat.format(new Date());
    }

    @Override
    public void open() {
        recordSchema = new RecordSchema();
        recordSchema.addField(new Field(FIELD_CONTENT, FieldType.STRING));
    }

    @Override
    public RecordEntry getRecordEntry(List<IotmoAppDO> iotmoAppDOS) {
        if (iotmoAppDOS == null || iotmoAppDOS.isEmpty()) {
            return null;
        }

        String jsonString = convert(iotmoAppDOS);
        if (jsonString != null) {
            RecordEntry recordEntry = new RecordEntry();
            TupleRecordData data = new TupleRecordData(recordSchema);
            data.setField(FIELD_CONTENT, jsonString);
            recordEntry.setRecordData(data);
            return recordEntry;
        }

        return null;
    }
}